package alibaba.testutils;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.PrintStream;
import java.util.Arrays;

import org.apache.commons.io.output.TeeOutputStream;

import alibaba.client.cli.AlibabaClient;

public class ClientTestUtils {

    public final static String BACKUP_FILE_SUCCESS_OUTPUT = "File backed up successfully\r\n";
    public final static String RESTORE_FILE_SUCCESS_OUTPUT = "File restored successfully\r\n";
    public final static String BACKUP_DIRECTORY_SUCCESS_OUTPUT = "Directory backed up successfully\r\n";
    public final static String RESTORE_DIRECTORY_SUCCESS_OUTPUT = "Directory restored successfully\r\n";
    public final static String SNAPSHOT_CREATE_SUCCESS_OUTPUT = "Snapshot created successfully\r\n";
    public final static String SNAPSHOT_DELETE_SUCCESS_OUTPUT = "Snapshot deleted successfully\r\n";
    public final static String UNEXPECTED_ERROR_OUTPUT = "Unexpected error occured\r\n";
    
    public static class ClientRunner {
        
        private String clientRoot;
        private String userName;
        
        public ClientRunner(String clientRoot, String userName) {
            this.clientRoot = clientRoot;
            this.userName = userName;
        }
        
        private String run(String ... args) {
            
            String[] arguments = Arrays.copyOf(args, args.length+2);
            arguments[args.length] = "-user";
            arguments[args.length+1] = userName;
            
            PrintStream sysOut = System.out;
            PrintStream sysErr = System.err;

            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            
            TeeOutputStream teeOut = new TeeOutputStream(sysOut, bos);
            TeeOutputStream teeErr=  new TeeOutputStream(sysErr, bos);
            PrintStream printTeeOut = new PrintStream(teeOut);
            PrintStream printTeeErr = new PrintStream(teeErr);
            
            System.setOut(printTeeOut);
            System.setErr(printTeeErr);
            
            AlibabaClient.main(arguments);
            
            System.setOut(sysOut);
            System.setErr(sysErr);
            
            return bos.toString();
            
        }
        
        public String backupFile(String path) {
            return run("bf", "-file", clientRoot + "\\" + path);
        }
        
        public String restoreFile(String path) {
            return run("rf", "-file", clientRoot + "\\" + path);
        }
        
        public String restoreFile(String path, int snapshot) {
            return run("rf", "-snapshot", String.valueOf(snapshot), "-file", clientRoot + "\\" + path);
        }
        
        public String backupDir() {
            return backupDir("");
        }
        
        public String backupDir(String path) {
            return run("bd", "-dir", clientRoot + "\\" + path);
        }
        
        public String restoreDir(String path) {
            return run("rd", "-dir", clientRoot + "\\" + path);
        }
        
        public String restoreDir(String path, int snapshot) {
            return run("rd", "-snapshot", String.valueOf(snapshot), "-dir", clientRoot + "\\" + path);
        }
        
        public String restoreDir() {
            return restoreDir("");
        }
        
        public String listDir(String path) {
            return run("ls", "-dir", clientRoot + "\\" + path);
        }
        
        public String listDir(String path, int snapshot) {
            return run("ls", "-snapshot", String.valueOf(snapshot), "-dir", clientRoot + "\\" + path);
        }
        
        public String listDir() {
            return listDir("");
        }
        
        public String checkDir(String path) {
            return run("ch", "-dir", clientRoot + "\\" + path);
        }
        
        public String checkDir() {
            return checkDir("");
        }
        
        public String createSnapshot(int snapshot) {
            return run("cs", "-snapshot", String.valueOf(snapshot));
        }
        
        public String deleteSnapshot(int snapshot) {
            return run("ds", "-snapshot", String.valueOf(snapshot));
        }
        
        public void cleanup() {
            for (File file : new File(clientRoot).listFiles()) {
                if (file.isFile()) {
                    file.delete();
                } else if (file.isDirectory()) {
                    FileTestUtils.deleteDirectory(file.getAbsolutePath());
                }
            }
        }
    }
}
